<chapter xml:id="utf8.writer.h">
<title><tt>__vic/utf8/writer.h</tt></title>

<chapter xml:id="utf8--writer">
<title><tt>utf8::writer</tt></title>

<code-block lang="C++"><![CDATA[
template<class ByteSWriter>
class utf8::writer
{
public:
    using byte_writer_type = ByteSWriter;
    ByteSWriter &get_byte_writer();
    const ByteSWriter &get_byte_writer() const;

    template<class... Args>
    explicit writer(Args&&... args); // since C++11

    writer(); // C++98 only
    explicit writer(ByteSWriter w); // C++98 only

    void write(unicode_t cp);
    void operator()(unicode_t cp);
};

template<class ByteSWriter>
utf8::writer<ByteSWriter> utf8::make_writer(ByteSWriter w);
]]></code-block>

<p>UTF-8 code points to byte sequence writer. <tt>ByteSWriter</tt>
which models <tt>swriter&lt;unsigned char></tt> (see <xref to="swriters"/>)
is used as a byte output.</p>

<section><title>Class members</title>

<synopsis>
<prototype>ByteSWriter &amp;get_byte_writer()</prototype>
<prototype>const ByteSWriter &amp;get_byte_writer() const</prototype>
<p>Returns reference to the used byte writer.</p>
</synopsis>

<synopsis>
<prototype>template&lt;class... Args>
explicit writer(Args&amp;&amp;... args) <badge>C++11</badge></prototype>
<p>Forwards all parameters to the used byte writer.</p>
</synopsis>

<synopsis>
<prototype>writer() <badge>C++98 only</badge></prototype>
<prototype>explicit writer(ByteSWriter r) <badge>C++98 only</badge></prototype>
<p>Constructors for C++98 mode.</p>
</synopsis>

<synopsis>
<prototype>void write(unicode_t cp)</prototype>
<prototype>void operator()(unicode_t cp)</prototype>
<p>Writes the specified code point according to UTF-8 encoding rules.
<tt>ByteSWriter::operator()</tt> is used to write individual bytes.</p>
</synopsis>

</section>

<section><title>Free functions</title>

<synopsis>
<prototype>template&lt;class ByteSWriter>
utf8::writer&lt;ByteSWriter> utf8::make_writer(ByteSWriter w)</prototype>
<p>Creates UTF-8 writer using specified <tt>ByteSWriter</tt>.</p>
</synopsis>

</section>

<section><title>Example</title>
<code-block lang="C++"><![CDATA[
#include<__vic/utf8/writer.h>
#include<__vic/swriters/string.h>
#include<string>
#include<vector>

// C++11
using utf8_string_writer = __vic::utf8::writer<__vic::string_swriter>;

// C++98
struct utf8_string_writer : __vic::utf8::writer<__vic::string_swriter>
{
    explicit utf8_string_writer(std::string &s)
        : __vic::utf8::writer<__vic::string_swriter>(__vic::string_swriter(s)) {}
};

std::string encode_utf8(const std::vector<__vic::unicode_t> &code_points)
{
    std::string utf8_res;
    utf8_string_writer w(utf8_res);
    for(auto cp : code_points) w(cp);
    return utf8_res;
}
]]></code-block>
</section>

</chapter>

</chapter>
